package com.jcompressor.utils;

/*
 * Copyright (c) 2011, Scott Carnett
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *       following disclaimerin the documentation and/or other materials provided with the distribution.
 *     * Neither the name of jcompressor nor the names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrBuilder;

import com.jcompressor.model.config.Configuration;

/**
 * @author Scott Carnett
 */
public class PropertyExpander {
	
	public static final String DEFAULT_PREFIX = "${";
	public static final String DEFAULT_SUFFIX = "}";
	
	private final Configuration configuration;
	private final String prefix;
	private final String suffix;
	private final Map<Object, Object> cache;
	
	public PropertyExpander(final Configuration configuration) {
		this.configuration = configuration;
		this.prefix = PropertyExpander.DEFAULT_PREFIX;
		this.suffix = PropertyExpander.DEFAULT_SUFFIX;
		this.cache = new HashMap<Object, Object>();
	}
	
	public PropertyExpander(final Configuration configuration, final String prefix, final String suffix) {
		this.configuration = configuration;
		this.prefix = prefix;
		this.suffix = suffix;
		this.cache = new HashMap<Object, Object>();
	}
	
	/**
	 * Clears the cache
	 */
	public void clearCache() {
		this.cache.clear();
	}
	
	/**
	 * Expands the properties
	 * @param source the source
	 * @return String with the result
	 */
	public String expandProperties(final String source) {
		if (StringUtils.isEmpty(source)) {
			return null;
		}
		
		String result = (String)this.cache.get(source);
		if (StringUtils.isEmpty(source) || StringUtils.isNotBlank(result)) {
			return result;
		}
		
		int firstIndex = StringUtils.indexOf(source, this.prefix);
		if (firstIndex == -1) {
			return source;
		}
		
		final StrBuilder buffer = new StrBuilder(source);
		while (firstIndex > -1) {
			final int lastIndex = StringUtils.indexOf(buffer.toString(), this.suffix);
			final int start = (firstIndex + this.prefix.length());
			if (firstIndex == 0) {
				final String key = StringUtils.substring(buffer.toString(), start, (start + lastIndex - this.prefix.length()));
				if (this.configuration.containsProperty(key)) {
					buffer.replace(firstIndex, (firstIndex + lastIndex + 1), this.configuration.getProperty(key).getValue());
				}
				else {
					break;
				}
			}
			else {
				final String key = StringUtils.substring(buffer.toString(), start, lastIndex);
				if (this.configuration.containsProperty(key)) {
					buffer.replace(firstIndex, (lastIndex + 1), this.configuration.getProperty(key).getValue());
				}
				else {
					break;
				}
			}
			
			firstIndex = StringUtils.indexOf(buffer.toString(), this.prefix);
		}
		
		result = buffer.toString();
		this.cache.put(source, result);
		return result;
	}
	
}